import java.util.PriorityQueue;
import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            char[] cc = in.nextLine().toCharArray();
            int[] hash = new int[128];
            for (char c : cc) {
                hash[c]++;
            }

            PriorityQueue<Integer> heap = new PriorityQueue<>();
            for (int i = 0; i < 128; i++) {
                if (hash[i] != 0) {
                    heap.offer(hash[i]);
                }
            }

            int ret = 0;
            while (heap.size() > 1) {
                int num1 = heap.poll();
                int num2 = heap.poll();
                ret += num1 + num2;
                heap.offer(num1+num2);
            }

            System.out.println(ret);
        }
    }
}
